/**
 * 	This file is part of GMGraphLib.
 * 
 * 	GMGraphLib is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  GMGraphLib is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with GMGraphLib.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.mendonca.gmgraphlib;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Gabriel Mendonça - gabrielgmendonca@gmail.com
 *
 */
public class AdjacencyMatrixEdges extends EdgeList {

	private static final long serialVersionUID = -3097041132947164746L;
	private int numberOfEdges = 0;
	
	public AdjacencyMatrixEdges(int numberOfVertices) {
		super(numberOfVertices);
	}

	/* (non-Javadoc)
	 * @see br.mendonca.gmgraphlib.EdgeList#addNewVertex()
	 */
	@Override
	protected void addNewVertex(int numberOfVertices) {
		List<Integer> copies = Collections.nCopies(numberOfVertices, 0);
		ArrayList<Integer> zeroesList = new ArrayList<Integer>(copies); 
		add(zeroesList);
	}

	@Override
	public void addEdge(int v1, int v2) {		
		List<Integer> list = get(v1 - 1);
		list.set(v2 - 1, 1);
		numberOfEdges++;
	}

	@Override
	public List<Integer> getNeighborsIndex(int vertexIndex) {
		List<Integer> neighbors = new LinkedList<Integer>();
				
		List<Integer> edges = get(vertexIndex - 1);
		for (int i = 0; i < edges.size(); i++) {
			if (edges.get(i) != 0) {
				neighbors.add(i + 1);
			}
		}
		
		return neighbors;
	}

	public boolean hasNext() {
		return numberOfEdges > 0;
	}

	@Override
	public Edge next() {
		numberOfEdges--;
		
		while (get(v1Index).get(v2Index) == 0) {
			updateIndexes();
		}
		
		return super.next();
	}
	
}
